package com.ljn.facade.imp;

import com.ljn.core.dto.common.Response;
import com.ljn.core.dto.common.command.projectcommand.ProCommand;
import com.ljn.core.dto.common.utils.assmbler.ProjectAssmbler;
import com.ljn.core.dto.common.utils.assmbler.RelationAssmbler;
import com.ljn.facade.ProFacade;
import com.ljn.service.ProService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.inject.Inject;
import javax.inject.Named;

/**
 * Created by liujunnan on 2017/11/15.
 */
@Named
public class ProFacadeImpl implements ProFacade{
    transient static final Logger LOGGER = LoggerFactory.getLogger(ProFacadeImpl.class);

    @Inject
    ProService proService;

    @Override
    public Response addPro(ProCommand proCommand) {
        Response response = new Response();
        try {
            Long flag = proService.addPro(ProjectAssmbler.toProject(proCommand));
            if (flag!=null)
            {
                response.setCode(Response.CodeEnum.SUCCESS.code);
                response.setMsg("发布项目成功！");
                response.setData(flag);//返回TRUE
            }
            else {
                response.setCode(Response.CodeEnum.FAILED.code);
                response.setMsg("发布项目失败！");
                response.setData("flag");
            }
        }
        catch (Exception e)
        {
            LOGGER.error(e.getMessage(), e);
            response.setCode(Response.CodeEnum.AUTHOR.code);
            response.setMsg("发布项目失败！");
            return response;
        }
        return response;
    }

    @Override
    public Response recPro(ProCommand proCommand) {
        Response response = new Response();
        try {
            String proId = proCommand.getId();
            Long flag = proService.revPro(RelationAssmbler.toRelation(proCommand),proId);
            if (flag > 0)
            {
                response.setCode(Response.CodeEnum.SUCCESS.code);
                response.setMsg("接收项目成功！");
                response.setData("flag");
            }else {
                response.setCode(Response.CodeEnum.FAILED.code);
                response.setMsg("接收项目失败");
                response.setData("flag");
            }

        }
        catch (Exception e)
        {
            LOGGER.error(e.getMessage(), e);
            response.setCode(Response.CodeEnum.AUTHOR.code);
            response.setMsg("接收项目失败！");
            return response;
        }
        return response;

    }

    @Override
    public Response changePro(ProCommand proCommand) {
        Response response = new Response();
        try{
            boolean resultProject = proService.changeProInfo(ProjectAssmbler.toProject(proCommand));
            if (resultProject) {
                response.setCode(Response.CodeEnum.SUCCESS.code);
                response.setMsg("修改成功！");
            } else {
                response.setCode(Response.CodeEnum.FAILED.code);
                response.setMsg("修改失败");
            }
        }
        catch (Exception e)
        {

            LOGGER.error(e.getMessage(), e);
            response.setCode(Response.CodeEnum.AUTHOR.code);
            response.setMsg("修改项目信息失败！");
            return response;
        }
        return response;
    }
}

